package cn.ytzb.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @Author XiaoMo
 * @Date 2022/3/15 11:11
 * @Version 1.0
 */
@Configuration
public class DatasourceUtil {

//    数据库配置

    @Resource
    private DataSource dataSource;

    //向spring的ioc容器注入此对象，名称就是方法名sqlSessionFactoryBean
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(){
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        try {
            //设置数据源
            sqlSessionFactoryBean.setDataSource(dataSource);
            //实体类别名
            sqlSessionFactoryBean.setTypeAliasesPackage("cn.ytzb.pojo");
            return sqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw  new RuntimeException();
        }
    }
}
